﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using Siser.Web.Security;

namespace Siser.Web.Shared
{
    public static class Mail
    {
        private static SmtpClient smtp = new SmtpClient()
        {
            Host = ConfigurationManager.AppSettings["ServidorSMTP"],
            Port = int.Parse(ConfigurationManager.AppSettings["PuertoServicioSMTP"]),
            EnableSsl = bool.Parse(ConfigurationManager.AppSettings["ActivarSSL"]),
            Timeout = int.Parse(ConfigurationManager.AppSettings["Timeout"])
        };

        public static bool Send(string from, string to, string subject, string body, bool bodyIsHTML)
        {
            MailMessage message = new MailMessage(from, to, subject, body){
                IsBodyHtml = bodyIsHTML
            };

            NetworkCredential credentials = new NetworkCredential()
            {
                UserName = ConfigurationManager.AppSettings["CuentaCorreoEnvio"],
                Password = Encryption.decryptAES(ConfigurationManager.AppSettings["ContrasenaCorreoEnvio"], "cchen.siser")
            };

            smtp.Credentials = credentials;
            try
            {
                smtp.Send(message);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static bool Send(string from, string to, string subject, string body, bool bodyIsHTML, byte[] pdfAttachmentStream)
        {
            Attachment attachment = new Attachment(new System.IO.MemoryStream(pdfAttachmentStream), "tarjeta_identificacion.pdf", "application/pdf");            
            MailMessage message = new MailMessage(from, to, subject, body)
            {
                IsBodyHtml = bodyIsHTML,
            };
            message.Attachments.Add(attachment);
            
            NetworkCredential credentials = new NetworkCredential()
            {
                UserName = ConfigurationManager.AppSettings["CuentaCorreoEnvio"],
                Password = Encryption.decryptAES(ConfigurationManager.AppSettings["ContrasenaCorreoEnvio"], "cchen.siser")
            };

            smtp.Credentials = credentials;
            try
            {
                smtp.Send(message);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}